package util;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

import play.libs.Json;
import jiyici.Constant.ErrorCode;
import jiyici.Constant.ResponseSpec;

/**
 * eg: { "action": actionId, "result": 0, //成功时返回userid, userid > 0 //失败或异常时返回0
 * //-1,用户不存在 //-2,密码不正确 //-3,用户被锁定（不能登陆） "errormsg": "系统出现异常", //失败时的错误提示信息
 * "data": { }
 * 
 * 
 * }
 */
public class ResponseUtil {

    public static ObjectNode success(final int action) {
        ObjectNode node = Json.newObject();
        node.put(ResponseSpec.KEY_ACTION, action);
        node.put(ResponseSpec.KEY_RESULT, ResponseSpec.SUCCESS);
        return node;
    }

    public static ObjectNode success(final int action, final JsonNode data) {
        ObjectNode note = success(action);
        if (data != null)
            note.put(ResponseSpec.KEY_DATA, data);
        return note;
    }

    public static ObjectNode fail(final int action) {
        ObjectNode node = Json.newObject();
        node.put(ResponseSpec.KEY_ACTION, action);
        node.put(ResponseSpec.KEY_RESULT, ResponseSpec.FAIL);
        return node;
    }

    public static ObjectNode fail(final int action, final int error) {
        ObjectNode node = fail(action);
        node.put(ResponseSpec.KEY_ERROR, error);
        return node;
    }

    public static ObjectNode failNotSupportAction(final int action) {
        return fail(action, ErrorCode.ERROR_ACTION_NOT_SUPPORTED);
    }
}
